/*
 * Copyright 2011-2016 YueJi.com All right reserved. This software is the confidential and proprietary information of
 * YueJi.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with YueJi.com.
 */
package com.bops.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bops.biz.UserTools;
import com.bops.biz.permission.Menu;
import com.bops.biz.permission.Role;

/**
 */
public class CommonServiceLocator {

    protected static ApplicationContext context;

    private static final Logger         logger = LoggerFactory.getLogger(UserTools.class);

    static {
        try {
            context = new ClassPathXmlApplicationContext(
                                                         new String[] { "classpath*:/META-INF/spring/biz/spring_*.xml" });
        } catch (RuntimeException e) {
            logger.error("", e);
            throw e;
        }
    }

    public static boolean hasInitFinish() {
        return context != null;
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public static Object getBean(String beanId) {

        return context.getBean(beanId);
    }

    public static UserService getUserService() {
        return (UserService) getBean("userService");
    }

    public static RoleService getRoleService() {
        if (context == null) {
            return null;
        }
        return (RoleService) context.getBean("roleService");
    }

    @SuppressWarnings("rawtypes")
    public static Map getAllRole() {
        if (menuList == null) {
            // 初始化一下
            getAllMenuList();
        }
        return context.getBeansOfType(Role.class);
    }

    @SuppressWarnings("rawtypes")
    public static List<Menu> getAllMenuList() {
        if (menuList == null) {
            synchronized (menuLockObject) {
                if (menuList == null) {
                    menuList = new ArrayList<Menu>();
                    Map map = context.getBeansOfType(Menu.class);
                    if (map != null) {
                        Iterator iterator = map.keySet().iterator();
                        while (iterator.hasNext()) {
                            Object key = iterator.next();
                            if (key != null) {
                                Object value = map.get(key);
                                if (value instanceof Menu) {
                                    Menu tmpMenu = (Menu) value;
                                    tmpMenu.setId((String) key);
                                    menuList.add(tmpMenu);
                                }
                            }
                        }
                    }
                }
            }

            // 把Role的Id也设置一下。
            Map allRolMap = context.getBeansOfType(Role.class);
            if (allRolMap != null) {
                Iterator iterator = allRolMap.keySet().iterator();
                while (iterator.hasNext()) {
                    Object key = iterator.next();
                    if (key != null) {
                        Object value = allRolMap.get(key);
                        if (value instanceof Role) {
                            Role tmpRole = (Role) value;
                            tmpRole.setId((String) key);
                        }
                    }
                }
            }

        }
        return menuList;
    }
    
    public static OrderService getOrderService() {
        return (OrderService) getBean("orderService");
    }
    
    public static ProductService getProductService() {
        return (ProductService) getBean("productService");
    }
    
    public static SupplierService getSupplierService() {
        return (SupplierService) getBean("supplierService");
    }

    
    public static CloudService getCloudService() {
        return (CloudService) getBean("cloudService");
    }
    
    public static NewsInfoService getNewsService() {
        return (NewsInfoService) getBean("newsService");
    }

    
    private static Object     menuLockObject = new Object();
    private static List<Menu> menuList;


}
